var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var CutList = require('../../components/cutList/index.js');
var app = getApp();
var PAGE_SIZE = 10; //活动分页每页数据条数

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,

        page: 2,
        pageLoading: false,
        pageEnd: false
    },

    onShareAppMessage: function(res) {
        return {
            title: app.getShareTitle() || app.getMarketTitle(),
            path: '/pages/index/index',
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },

    onLoad(options) {
        common.initApp({
            extSuc: () => {
                this._fetch();
                this._cutList = new CutList({
                    scope: 'cutList',
                    page: this,
                    showType: 1
                });
            },
            loginSuc: () => {
                this.$loginSuc();
            },
            sysInfoSuc: (sysInfo) => {
                this.setData({
                    winHei: app.getWinHei(),
                });
            },
        });
    },

    onPullDownRefresh() {
        this._fetch(() => {
            wx.stopPullDownRefresh();
        });
        this._cutList && this._cutList.init();
    },

    onReachBottom() {
        this._getActList();
    },

    /**
     * 拉取页面数据
     * @return {[type]} [description]
     */
    _fetch(cb) {
        ajax.G({
            url: '/market/Home/index',
            data: {},
            success: (res) => {
                var {
                    bannerList = [],
                        actList = []
                } = res.result;
                var pageEnd = actList.length < PAGE_SIZE;

                bannerList = bannerList.map((item) => {
                    return {
                        imgUrl: item.image_url
                    }
                });

                this.setData({
                    dataLoaded: true,
                    bannerList,
                    actList,
                    pageEnd,
                });

                cb && cb();
            },
            fail: () => {
                cb && cb();
            }
        });
    },

    /**
     * 扫码下单
     * @return {[type]} [description]
     */
    _scanCode() {
        wx.scanCode({
            success: (res) => {

                const url = res.path;
                let urlArr = [];

                console.log(url);

                if (url.indexOf('?scene=') > -1) {

                    urlArr = url.split('?');

                    this._parmas = urlArr[1].replace('scene=', '');

                    wx.navigateTo({
                        url: '/' + urlArr[0] + '?' + decodeURIComponent(this._parmas)
                    });

                } else {

                    wx.navigateTo({
                        url: '/' + url
                    })

                }
            }
        });
    },

    /**
     * 异步加载活动列表，第二页起
     * @return {[type]} [description]
     */
    _getActList() {
        var {
            page: curPage,
            pageEnd,
        } = this.data;

        if (pageEnd) {
            return;
        }

        this.setData({
            pageLoading: true,
        });
        ajax.G({
            url: '/market/Home/getActData',
            data: {
                page: curPage,
            },
            success: (res) => {
                var allList = this.data.actList || [];
                var {
                    actList = []
                } = res.result;

                this.setData({
                    actList: allList.concat(actList),
                    page: curPage + 1,
                    pageLoading: false,
                    pageEnd: actList.length < PAGE_SIZE
                });
            }
        });
    }
}));